const util = require("../../utils/util");
var app = getApp();
Page({
    data: {
        userInfo: app.globalData.userInfo,
        carId: 0,
        dayTime: '',
        nickname: '',
        timeRanges: [],
        initCount: 0,

        //添加时段
        addRangeTimeTitle: '',
        showAdd: false,
        addDayStart: '06:30',
        addDayEnd: '06:30',
        selectId: 0,
        showAddActions: [{
            name: '确定',
            color: '#ed3f14',
            loading: false
        }, {
            name: '关闭'
        }],

        //删除
        visibleShowDelete: false,
        deleteActions: [{
            name: '删除',
            color: '#ed3f14'
        }],
        isSaving: false
    },
    onLoad: function (options) {
        var self = this;
        //检查token是否存在
        var userInfo = util.getStorageSync("userInfo");
        if (userInfo != null && app.globalData.userInfo) {
            self.setData({
                userInfo: app.globalData.userInfo,
                isLogin: true
            });
        }
        var id = options.carId;
        if (id && id > 0) {
            this.setData({
                carId: id
            });
            this.getDayPlanInfo(id);
        }
    },
    onShow: function () {},
    getDayPlanInfo: function (id) {
        var self = this;
        // 请求数据，并渲染
        wx.request({
            url: app.globalData.api + '/CarNotice/AddDayPlanInfo',
            data: {
                token: app.globalData.userInfo.token,
                id: id
            },
            method: 'GET',
            success: function (res) {
                if (res.data.resultCode == 0) {
                    if (res.data.data) {
                        self.setData({
                            dayTime: res.data.data.dayTime,
                            nickname: res.data.data.nickname,
                            timeRanges: res.data.data.timeRanges,
                            initCount: res.data.data.timeRanges.length
                        });
                    }
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    });
                }
            }
        });
    },
    handleSaveClick: function () {
        //拼接参数
        var self = this;
        if (!self.data.isLogin) {
            wx.showModal({
                title: '温馨提示',
                content: '您还没有登录，将无法获取用户标识，点击去登录前往登录。',
                confirmText: '去登录',
                success: function (res) {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/login/index'
                        });
                    }
                }
            });
            return;
        }
        if (self.data.isSaving || self.data.timeRanges.length < 1) {
            return;
        }
        self.setData({
            isSaving: true
        });
        var timeRanges = [];
        self.data.timeRanges.forEach(element => {
            timeRanges.push(element.start + '-' + element.end);
        });
        wx.request({
            url: app.globalData.api + '/CarNotice/SaveDayPlan',
            data: {
                token: app.globalData.userInfo.token,
                carId: self.data.carId,
                dayTime: self.data.dayTime,
                timeRanges: timeRanges
            },
            method: 'POST',
            success: function (res) {
                if (res.data.resultCode == 0) {
                    wx.showToast({
                        title: '保存成功',
                        icon: 'success',
                        duration: 2000
                    });
                    wx.navigateBack();
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    });
                }
                self.setData({
                    isSaving: false
                });
            },
            fail: function () {
                wx.showToast({
                    title: '网络错误',
                    icon: 'error',
                    duration: 2000
                });
            }
        });
    },
    bindAddDayStartTimeChange: function (e) {
        this.setData({
            addDayStart: e.detail.value
        });
    },
    bindAddDayEndTimeChange: function (e) {
        this.setData({
            addDayEnd: e.detail.value
        });
    },
    handleClickEdit: function (e) {
        var id = e.currentTarget.dataset.id;
        var timeRanges = this.data.timeRanges;
        for (let i = 0; i < timeRanges.length; i++) {
            let temp = timeRanges[i];
            if (temp.id == id) {
                this.setData({
                    addRangeTimeTitle: '修改时段',
                    showAdd: true,
                    addDayStart: temp.start,
                    addDayEnd: temp.end,
                    selectId: temp.id
                });
                return;
            }
        }
    },
    handleClickDelete: function (e) {
        var index = e.currentTarget.dataset.id;
        var timeRanges = this.data.timeRanges;
        var tempRanges = [];
        timeRanges.forEach(element => {
            if (element.id == index) {
                return;
            }
            tempRanges.push(element);
        });
        this.setData({
            timeRanges: tempRanges
        });
    },
    handleToAddDailyHistory: function (e) {
        this.setData({
            addRangeTimeTitle: '添加时段',
            showAdd: true,
            addDayStart: '06:30',
            addDayEnd: '06:30',
            selectId: 0
        });
    },
    addHandlerClick: function ({
        detail
    }) {
        var self = this;
        console.log(detail)
        if (detail.index == 1) {
            self.setData({
                addRangeTimeTitle: '',
                showAdd: false,
                addDayStart: '',
                addDayEnd: '',
                selectId: 0
            });
        } else {
            var index = self.data.initCount;
            var timeRanges = self.data.timeRanges;
            if (self.data.selectId > 0) {
                for (let i = 0; i < timeRanges.length; i++) {
                    let temp = timeRanges[i];
                    if (temp.id == self.data.selectId) {
                        temp.end = self.data.addDayEnd;
                        temp.start = self.data.addDayStart;
                        break;
                    }
                }
            } else {
                index = index + 1;
                timeRanges.push({
                    end: self.data.addDayEnd,
                    id: index,
                    start: self.data.addDayStart
                });
            }

            self.setData({
                addRangeTimeTitle: '',
                showAdd: false,
                addDayStart: '',
                addDayEnd: '',
                selectId: 0,
                initCount: index,
                timeRanges: timeRanges
            });
        }
    },
    onShareAppMessage() {
        return {
            title: '记录你的一点一滴~',
            desc: '记录你的一点一滴~',
            path: 'pages/index/index',
            imageUrl: app.globalData.shareImgUrl
        };
    }
});